/****************************************************************************
 * Copyright (c) 2008, 2009 Andreas Unger and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Andreas Unger - initial API and implementation 
 ****************************************************************************/

package org.eclipselabs.damos.diagram.ui.properties;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
import org.eclipselabs.damos.diagram.core.internal.commands.SetArgumentValueCommand;
import org.eclipselabs.damos.diagram.ui.internal.util.PropertySectionUtil;
import org.eclipselabs.damos.dml.Argument;
import org.eclipselabs.damos.dml.DMLFactory;
import org.eclipselabs.damos.dml.ExpressionParameter;
import org.eclipselabs.damos.dml.ExpressionSpecification;
import org.eclipselabs.damos.dml.ValueSpecification;
import org.eclipselabs.damos.typesystem.Enumeration;
import org.eclipselabs.damos.typesystem.EnumerationLiteral;

/**
 * @author Andreas Unger
 *
 */
public class EnumerationParameterCompositeDelegate extends ComboPropertyCompositeDelegate implements IParameterCompositeDelegate {

	private Argument argument;
	private List<String> propertyValueStrings;
	
	/**
	 * 
	 */
	public EnumerationParameterCompositeDelegate(TabbedPropertySheetWidgetFactory widgetFactory, Argument argument) {
		super(widgetFactory);
		if (!(argument.getParameter() instanceof ExpressionParameter
				&& ((ExpressionParameter) argument.getParameter()).getType() instanceof Enumeration)) {
			throw new IllegalArgumentException();
		}
		this.argument = argument;
	}
	
	public void refreshParameter() {
		boolean selected = false;
		ValueSpecification valueSpec = argument.getValue();
		if (valueSpec != null) {
			String value = valueSpec.stringValue();
			String[] segments = value.split("\\.");
			if (segments.length > 0) {
				String literalName = segments[segments.length - 1];
				EnumerationLiteral literal = getEnumeration().getLiteral(literalName);
				if (literal != null) {
					getControl().select(getEnumeration().getLiterals().indexOf(literal));
					selected = true;
				}
			}
		}
		if (!selected) {
			getControl().setText(argument.getValue().stringValue());
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipselabs.damos.diagram.ui.properties.ComboPropertySectionDelegate#updatePropertyValue()
	 */
	protected void updatePropertyValue() {
		EnumerationLiteral literal = getEnumeration().getLiteralByLiteral(getControl().getText());
		if (literal != null) {
			ExpressionSpecification expressionSpec = DMLFactory.eINSTANCE.createExpressionSpecification();
			expressionSpec.setExpression(literal.getQualifiedName());
			executeOperation(new SetArgumentValueCommand(argument, expressionSpec));
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipselabs.damos.diagram.ui.properties.AbstractLabeledPropertySectionDelegate#getLabelText()
	 */
	protected String getLabelText() {
		return PropertySectionUtil.formatName(argument.getParameter().getName()) + ":";
	}

	protected List<String> getPropertyValueStrings() {
		if (propertyValueStrings == null) {
			propertyValueStrings = new ArrayList<String>();
			for (EnumerationLiteral literal : getEnumeration().getLiterals()) {
				propertyValueStrings.add(literal.getLiteral());
			}
		}
		return propertyValueStrings;
	}
	
	private ExpressionParameter getParameter() {
		return (ExpressionParameter) argument.getParameter();
	}
	
	private Enumeration getEnumeration() {
		return (Enumeration) getParameter().getType();
	}
	
}
